﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BF.IBLL.Dictionary;
using BF.Model;
using BF.IDAL.Dictionary;
using System.Linq.Expressions;
using BF.Infrastructure;
using BF.BLL.BaseSystem;

namespace BF.BLL.Dictionary
{
    public class Dic_DictBLL : Idic_DictBLL
    {
        private readonly Idic_DictDAL iDal;
        private readonly IUnitOfWork<WSJD_BaseFrameworkEntities> uwork;
        public Dic_DictBLL(IUnitOfWork<WSJD_BaseFrameworkEntities> uwork, Idic_DictDAL iDal)
        {
            this.iDal = iDal;
            this.uwork = uwork;
        }

        #region Idic_DictBLL 成员

        public dic_Dict GetModelByCondition(Expression<Func<dic_Dict, bool>> where)
        {
            return iDal.GetModelByCondition(where);
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool Add(dic_Dict model)
        {
            iDal.Add(model);
            int count = uwork.Commit();
            return count > 0;
        }

        /// <summary>
        /// 获取一列数据
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public List<dic_Dict> GetList(Expression<Func<dic_Dict, bool>> where)
        {
            return iDal.GetList(where);
        }

        /// <summary>
        /// 获取字典列表(分页)
        /// </summary>
        /// <param name="where">条件</param>
        /// <param name="orderBy">排序字段</param>
        /// <param name="pageModel">分页实体</param>
        /// <param name="isAscending">是否升序排列</param>
        /// <returns></returns>
        public List<dic_Dict> GetList(Expression<Func<Model.dic_Dict, bool>> where, Expression<Func<dic_Dict, object>> orderBy, PageModel pageModel, bool isAscending)
        {
            return iDal.GetList(where, orderBy, pageModel, isAscending);
        }

        /// <summary>
        /// 获取排序后的集合 Ordery By sName Desc
        /// </summary>
        /// <param name="whereLam"></param>
        /// <returns></returns>
        public List<dic_Dict> GetDescList(Expression<Func<dic_Dict, bool>> whereLam)
        {
            return iDal.GetDescList(whereLam);
        }

        /// <summary>
        /// 获取数据条数
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public int GetRowCount(Expression<Func<dic_Dict, bool>> where)
        {
            return iDal.GetRowCount(where);
        }

        /// <summary>
        /// 获取一个实体
        /// </summary>
        /// <param name="gID"></param>
        /// <returns></returns>
        public dic_Dict GetModel(Guid gID)
        {
            return iDal.GetModelByCondition(P => P.gKey.Equals(gID));
        }

        /// <summary>
        /// 获取一个实体
        /// </summary>
        /// <param name="sCode">字典编码</param>
        /// <returns></returns>
        public dic_Dict GetModel(string sCode)
        {
            return iDal.GetModelByCondition(p => p.sCode.Equals(sCode));
        }

        /// <summary>
        /// 修改一个实体
        /// </summary>
        /// <returns></returns>
        public bool Update()
        {
            int count = uwork.Commit();
            if (count > 0)
                return true;
            else
                return false;
        }

        /// <summary>
        /// 删除一个实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Delete(Model.dic_Dict model)
        {
            iDal.Delete(model);
            int count = uwork.Commit();
            if (count > 0)
            {

                return true;
            }
            else
                return false;
        }
        #endregion


        public string Up(string gKey, int iSort, string gGENKey)
        {
            dic_Dict GENModel = GetModel(new Guid(gGENKey));
            if (GENModel != null)
            {
                dic_Dict model = this.GetModel(new Guid(gKey));
                if (iSort > 0)
                {
                    int? newSort = iDal
                      .GetList(P => P.sTableCode == GENModel.sCode && P.iSort < iSort)
                      .Max(P => P.iSort);
                    if (newSort == null)
                        return "已是第一条,无法向上!";
                    dic_Dict upModel = iDal
                        .GetModelByCondition(P => P.sTableCode == GENModel.sCode
                            && P.iSort == newSort);
                    model.iSort = newSort;
                    upModel.iSort = iSort;
                    if (uwork.Commit() > 0)
                        return "操作成功!";
                    else
                        return "操作失败!";
                }
            }
            return "已是第一条,无法向上!";
        }

        public string Down(string gKey, int iSort, string gGENKey)
        {
            dic_Dict GENModel = GetModel(new Guid(gGENKey));
            if (GENModel != null)
            {
                dic_Dict model = this.GetModel(new Guid(gKey));
                int? newSort = iDal
                      .GetList(P => P.sTableCode == GENModel.sCode && P.iSort > iSort)
                      .Min(P => P.iSort);
                if (newSort != null)
                {
                    dic_Dict upModel = iDal
                        .GetModelByCondition(P => P.sTableCode == GENModel.sCode
                            && P.iSort == newSort);

                    model.iSort = newSort;
                    upModel.iSort = iSort;
                    if (uwork.Commit() > 0)
                        return "操作成功!";
                    else
                        return "操作失败!";
                }
            }
            return "已是最后一条,无法向下!";
        }

        #region Idic_DictBLL 成员


        public bool NewSortMethod(string[] gKeys, string[] iSorts)
        {
            for (int i = 0; i < gKeys.Length; i++)
            {
                string gkey = gKeys[i].Trim();
                int OUTINT;
                if (!string.IsNullOrEmpty(gkey))
                {
                    dic_Dict tempModel = iDal.GetModelByCondition(P => P.gKey == new Guid(gkey));
                    if (tempModel != null && int.TryParse(iSorts[i], out OUTINT) && tempModel.iSort != OUTINT)
                    { tempModel.iSort = OUTINT; }
                }
            }
            return uwork.Commit() > 0;
        }

        #endregion
    }
}
